from httprunner import HttpRunner,Config,Step,RunTestCase,RunRequest
from common.ConfigpaserRead import ConRead
from business.crmeb_login_test import TestCaseLogin as TL
# 第一步需要处理公共的url
base_url = ConRead("config.ini").get_url_value('base')

class API_Start(HttpRunner):
    config = Config("test start..").base_url(base_url)
    teststeps = [
        RunTestCase("login start")
            .call(TL)
            .export(*['token',])
    ] # 把登录文件写在business里，这里进行call调用

# 重置token
def reset_token(token=TL):
    """
    比如一个电商网站，会有后台管理员、C端用户、游客不同的身份
    不同的身份就代表有不同的token权限
    所以这里默认是传入的TL,方便后面去替换token权限
    """
    API_Start.teststeps.clear() # 先把所有的token清空掉
    API_Start.teststeps.append(
        Step(
            RunTestCase("登录").call(token)
            .export(*["token",])
        )
    )

headers_dict = {'Content-Type': 'application/json;charset=UTF-8',"Authori-zation":"Bearer ${token}"}
def requests_send(api_name,method,path,body=None,headers=headers_dict):
    if method == 'post':
        return RunRequest(api_name).\
            post(path).\
            with_headers(**headers).\
            with_json(body).\
            extract().\
            with_jmespath("body","res_data") # 把所有的响应体都提出来，给到后面去使用
    elif method =='get': # 如果是get请求，请求参数拼接在url后面
        return RunRequest(api_name). \
            get(path). \
            with_headers(**headers).\
            extract().\
            with_jmespath("body", "res_data")
    elif method=='delete':
        return RunRequest(api_name). \
            delete(path). \
            with_headers(**headers). \
            extract(). \
            with_jmespath("body", "res_data")


def Step_Obj(run_obj):
    return Step(
        run_obj
    )


def start_api(objct):
    API_Start.teststeps.extend(objct)
    obj = API_Start()
    obj.test_start()
    res = obj.with_export(['token','res_data']).get_export_variables()
    return res  # 会返回一个字典格式

















